package com.qk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BussinesServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;

    @Autowired
    public CustomerMapper customerMapper;

    @Override
    public PageResult<Business> listBusiness(BusinessDto businessDto) {
        Page<Business> page = businessMapper.listBusinesses(new Page< Business>(businessDto.getPage(),businessDto.getPageSize()),businessDto);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    @Override
    public Business getBusinessById(Integer id) {
        return businessMapper.getBusinessById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void trackBusiness(Business business) {
        //根据id更新商机的基本信息
        business.setStatus(3);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        //添加商机的跟进记录-插入一条数据
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        businessTrackRecord.setTrackStatus(business.getTrackStatus());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    //踢回公海
    @Override
    public void back(Integer id) {
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);
    }

    //转成客户
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toCustomer(Integer id) {
        //- 根据商机ID查询上信息  更新商机状态。
        Business business = businessMapper.selectById(id);
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        //- 添加客户表-插入一条数据
        Customer customer = new Customer();
        customer.setId( null);
        customer.setPhone(business.getPhone());
        customer.setChannel(business.getChannel());
        customer.setName(business.getName());
        customer.setGender(business.getGender());
        customer.setAge(business.getAge());
        customer.setWechat(business.getWechat());
        customer.setQq(business.getQq());
        customer.setDegree(business.getDegree());
        customer.setJobStatus(business.getJobStatus());
        customer.setSubject(business.getSubject());
        customer.setCourseId(business.getCourseId());
        customer.setBusinessId(business.getId());
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> poolList(BusinessDto businessDto) {
        Page<Business> page = new Page<>(businessDto.getPage(),businessDto.getPageSize());

        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(businessDto.getBusinessId() != null, Business::getId,businessDto.getBusinessId())
                .like(businessDto.getName() != null && businessDto.getName().isEmpty(), Business::getName,businessDto.getName())
                .like(businessDto.getPhone() != null && businessDto.getPhone().isEmpty(), Business::getPhone,businessDto.getPhone())
                .eq(businessDto.getStatus() != null, Business::getStatus,businessDto.getStatus())
                .eq(Business::getStatus,4)
                .orderByDesc(Business::getCreateTime);
        page = this.page(page,queryWrapper);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }


}
